<?php
/**
 * Kelas user
 */
class pengelolaan_inventaris extends CI_Controller {
	
	 //Konstruktor
	 
	function __construct()
	{
		parent::__construct();
		$this->load->model('barang_model');	
		$this->load->library('form_validation');
		$this->cek_session();
	}
	
	function cek_session()
	 {	
		$kode_role = $this->session->userdata('id_jabatan');
		if($kode_role == NULL)
		{
			//redirect('login/login_ulang');
			redirect('login');
		}//else if($kode_role != 1){
			//redirect('login/alert');
		//}
	 }
	
	/**
	 * Menampilkan tabel daftar barang
	 */
	 function index($id_org)
	 {
			//$this->session->set_flashdata('item', 'value');$this->session->flashdata('item');
			if($id_org == $this->session->userdata('id_organisasi') || $this->session->userdata('id_jabatan') == 1){
				$data_konten['sukses']=$this->session->flashdata('sukses');
				$data_konten['gagal']=$this->session->flashdata('gagal');
				$data_konten['barang']=$this->barang_model->ambil_daftar_barang($id_org);			
				$data["konten"] = $this->load->view("daftar_barang", $data_konten, TRUE);
				$data["menu"] = 'inventaris';
				if($this->session->userdata('id_jabatan') != 1){
					$data["sub_menu"] = 'daftar_inventaris';
				}else {
					if($id_org == 1){
						$data["sub_menu"] = 'daftar_inventaris_itsonline';
					} else {
						$data["sub_menu"] = 'daftar_inventaris_itstv';						
					}
				}
				$this->load->view("main", $data);
			} else {
				redirect('home');
			}
	 }

	 function detail_barang($id_barang)
	 {
		if($this->barang_model->ambil_detail_barang($id_barang)->row()->id_organisasi == $this->session->userdata('id_organisasi') || $this->session->userdata('id_jabatan') == 1){
			$id_org = $this->barang_model->ambil_detail_barang($id_barang)->row()->id_organisasi;
			$data_konten['id_org'] = $id_org;
			$data_konten['histori_peminjaman_barang']=$this->barang_model->histori_peminjaman_barang($id_barang);
			$data_konten['nama_barang']= $this->barang_model->ambil_detail_barang($id_barang)->row()->nama_barang;
			$data_konten['keterangan']= $this->barang_model->ambil_detail_barang($id_barang)->row()->keterangan;
			$data_konten['row']=$this->barang_model->ambil_detail_barang($id_barang)->row();
			$data["konten"] = $this->load->view("detail_barang", $data_konten, TRUE);
			$data["menu"] = 'inventaris';
			if($this->session->userdata('id_jabatan') != 1){
					$data["sub_menu"] = 'daftar_inventaris';
				}else {
					if($id_org == 1){
						$data["sub_menu"] = 'daftar_inventaris_itsonline';
					} else {
						$data["sub_menu"] = 'daftar_inventaris_itstv';						
					}
				}
			$this->load->view("main", $data);
		} else {
				redirect('home');
		}
	 }
	
	//proses penambahan barang
	function add_process()
	{
		$role_jabatan = $this->session->userdata('id_jabatan');
		if($role_jabatan == '2' || $role_jabatan == '4'){
		if($this->cek_validasi() == true)
		{
			$field_name = 'foto_barang';
            $config['upload_path'] = "gallery/barang";
            $config['allowed_types'] = 'gif|jpg|png';
            //$config['allowed_types'] ='jpg';
				
			//create folder
			if(!is_dir($config['upload_path'])){
				mkdir($config['upload_path'], 0777);
				//mkdir($config['upload_path2'], 0777);
			}

            $this->load->library('upload', $config);
            $files = $this->upload->do_upload($field_name);
			$data = $this->upload->data($field_name); //get file_name $field_name
			
			if (!$files) {
				$file_name = 'default.jpg';
			}
			else {				
				$file_name = $data['file_name'];
			}
					
            $path[0] = 'gallery/barang/' . $file_name;
            $path[1] = $file_name;
            //return $path;
			
			$barang = array('kode_barang' 		=> $this->input->post('kode_barang'),
							'nama_barang' 		=> $this->input->post('nama_barang'),
							'foto_barang'	=>	$path[0],
							'keterangan'  		=> $this->input->post('keterangan'),
							'kondisi_inventaris'	=> $this->input->post('kondisi_barang'),
							'status_barang'	=> 'Tidak Dipinjam',
							'id_organisasi'		=> $this->session->userdata('id_organisasi')
							);
			$barang = str_replace("'", "\'", $barang);
			$this->barang_model->tambah_barang($barang);
			$nama_barang = $this->input->post('kode_barang').' ('.$this->input->post('nama_barang').' - '.$this->input->post('keterangan').')';
			$this->session->set_flashdata('sukses', 'Data inventaris barang '.$nama_barang.' berhasil ditambahkan.');
			redirect('pengelolaan_inventaris');
		}
		else{			
			$data["konten"] = $this->load->view("form_tambah_barang", '', TRUE);			
			$data["menu"] = 'inventaris';
			$data["sub_menu"] = 'tambah_inventaris';
			$this->load->view("main", $data);
		}
		} else {
			redirect('home');
		}
	}
	
	//mengubah isi barang
	function edit_proses($kode_barang)	{
		
		$role_jabatan = $this->session->userdata('id_jabatan');			
		$id_organisasi = $this->barang_model->ambil_detail_barang($kode_barang)->row()->id_organisasi;
		
		if(($role_jabatan == '2' || $role_jabatan == '4') && $id_organisasi == $this->session->userdata('id_organisasi')){
			
		if($this->cek_validasi() == true)
		{
			$kode_barang_lama = $this->input->post('kode_barang_lama');
			
				$field_name = 'foto_barang';
				$config['upload_path'] = "gallery/barang";
				$config['allowed_types'] = 'gif|jpg|png';
				//$config['allowed_types'] ='jpg';
					
				//create folder
				if(!is_dir($config['upload_path'])){
					mkdir($config['upload_path'], 0777);
					//mkdir($config['upload_path2'], 0777);
				}

				$this->load->library('upload', $config);
				$files = $this->upload->do_upload($field_name);
				$data = $this->upload->data($field_name); //get file_name $field_name
				
				if (!$files) {
					$path[0] = $this->barang_model->ambil_detail_barang($kode_barang_lama)->row()->foto_barang;
				}
				else {				
					$path[0] = 'gallery/barang/' . $data['file_name'];
				}
			
			$barang_baru = array('kode_barang' 		=> $this->input->post('kode_barang'),
							'nama_barang' 		=> $this->input->post('nama_barang'),
							'foto_barang'	=>	$path[0],
							'keterangan'  		=> $this->input->post('keterangan'),
							'kondisi_inventaris'	=> $this->input->post('kondisi_barang'),
							'id_organisasi'		=> $this->session->userdata('id_organisasi')
						);
			
			$this->barang_model->edit_barang($barang_baru, $kode_barang_lama);			
			$nama_barang = $this->input->post('kode_barang').' ('.$this->input->post('nama_barang').' - '.$this->input->post('keterangan').')';
			$this->session->set_flashdata('sukses', 'Data inventaris barang '.$nama_barang.' berhasil diedit.');
			//$this->session->set_flashdata('item', 'value');$this->session->flashdata('item');			
			$id_org = $this->barang_model->ambil_detail_barang($this->input->post('kode_barang'))->row()->id_organisasi;
			redirect('pengelolaan_inventaris/index/'.$id_org);
		}
		else{
			$barang_lama = $this->barang_model->ambil_detail_barang($kode_barang);
			foreach($barang_lama->result() as $data_barang){
				$data_konten['kode_barang'] 		= $data_barang->kode_barang;
				$data_konten['nama_barang']  		= $data_barang->nama_barang;
				$data_konten['keterangan']			= $data_barang->keterangan;
				$data_konten['kondisi_barang']		= $data_barang->kondisi_inventaris;
				$data_konten['foto_barang']			= $data_barang->foto_barang;
			}					
			$data["konten"] = $this->load->view("form_edit_barang", $data_konten, TRUE);
			$data["menu"] = 'inventaris';
			$data["sub_menu"] = 'daftar_inventaris';
			$this->load->view("main", $data);
		}
		} else {
			redirect('home');
		}
	}
	
	function hapus_inventaris($id_barang){
		$role_jabatan = $this->session->userdata('id_jabatan');		
		$id_org = $this->barang_model->ambil_detail_barang($id_barang)->row()->id_organisasi;
		if(($role_jabatan == '2' || $role_jabatan == '4') && $id_org == $this->session->userdata('id_organisasi')){
		$nama_barang = $this->barang_model->ambil_detail_barang($id_barang)->row()->kode_barang.' ('.$this->barang_model->ambil_detail_barang($id_barang)->row()->nama_barang.' - '.$this->barang_model->ambil_detail_barang($id_barang)->row()->keterangan.')';
		$this->barang_model->hapus_barang($id_barang);
		$this->session->set_flashdata('sukses', 'Data inventaris barang '.$nama_barang.' berhasil dihapus.');
		redirect('pengelolaan_inventaris/index/'.$id_org);
		} else {
			redirect('home');			
		}
	}
	
	// Set validation rules
	function cek_validasi()
	{	
		$this->form_validation->set_rules('kode_barang', 'kode barang', 'required');
		$this->form_validation->set_rules('nama_barang', 'nama barang', 'required');
		$this->form_validation->set_rules('keterangan', 'keterangan', 'required');
		$this->form_validation->set_rules('kondisi_barang', 'kondisi barang', 'required');
		$this->form_validation->set_message('required', '<label for="error" generated="true" class="error" style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kolom %s harus diisi !!</label>');
		return $this->form_validation->run();
	}
} // END user class

/* End of file user.php */
/* Location: ./system/application/controllers/manajemen_user.php */
